package com.bestom.bluetoothlock.activity;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.fragment.DeviceFragment;
import com.bestom.bluetoothlock.fragment.MineFragment;
import com.bestom.bluetoothlock.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    private DeviceFragment deviceFragment;
    private MineFragment mineFragment;
    private FragmentManager fragmentManager;
    private int index = 0;
    private long mExitTime;

    @BindView(R.id.tv_device)
    TextView tv_device;
    @BindView(R.id.tv_mine)
    TextView tv_mine;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        if (Build.VERSION.SDK_INT >= 23 && checkSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == PackageManager.PERMISSION_DENIED) {
            requestPermissions(new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 133);
        }
    }

    private void initView() {
        this.fragmentManager = getSupportFragmentManager();
    }

    private void initBottom() {
        this.tv_device.setSelected(false);
        this.tv_mine.setSelected(false);
    }

    private void hintfragment(FragmentTransaction fragmentTransaction) {
        DeviceFragment deviceFragment2 = this.deviceFragment;
        if (deviceFragment2 != null) {
            fragmentTransaction.hide(deviceFragment2);
        }
        MineFragment mineFragment2 = this.mineFragment;
        if (mineFragment2 != null) {
            fragmentTransaction.hide(mineFragment2);
        }
    }

    private void showFragment(int i) {
        initBottom();
        FragmentTransaction beginTransaction = this.fragmentManager.beginTransaction();
        hintfragment(beginTransaction);
        switch (i) {
            case 0:
                this.tv_device.setSelected(true);
                DeviceFragment deviceFragment2 = this.deviceFragment;
                if (deviceFragment2 != null) {
                    beginTransaction.show(deviceFragment2);
                    break;
                } else {
                    this.deviceFragment = new DeviceFragment();
                    beginTransaction.add(R.id.ll_main, (Fragment) this.deviceFragment);
                    break;
                }
            case 1:
                this.tv_mine.setSelected(true);
                MineFragment mineFragment2 = this.mineFragment;
                if (mineFragment2 != null) {
                    beginTransaction.show(mineFragment2);
                    break;
                } else {
                    this.mineFragment = new MineFragment();
                    beginTransaction.add(R.id.ll_main, (Fragment) this.mineFragment);
                    break;
                }
            default:
        }
        beginTransaction.commitAllowingStateLoss();
    }

    public void exit() {
        if (this.index != 0) {
            showFragment(0);
            this.index = 0;
        } else if (System.currentTimeMillis() - this.mExitTime > 2000) {
            ToastUtil.toast(getResources().getString(R.string.click_quit));
            this.mExitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }
    @Override
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i != 4 || keyEvent.getRepeatCount() != 0) {
            return super.onKeyDown(i, keyEvent);
        }
        exit();
        return true;
    }

    @OnClick({R.id.tv_device_layout, R.id.tv_setting_layout})
    public void onClickView(View view) {
        int id = view.getId();
        if (id == R.id.tv_device_layout) {
            this.index = 0;
            showFragment(this.index);
        } else if (id == R.id.tv_setting_layout) {
            this.index = 1;
            showFragment(this.index);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        showFragment(this.index);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}